package cn;
/*
* 定义一个玩家类
* */
import javafx.scene.input.KeyCode;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Player extends KeyAdapter {

    GameFrame frame;

    public Player(GameFrame frame) {
        this.frame = frame;
    }

    /*
    * 控制飞机
    * */
    //  键盘按下
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        //System.out.println(keyCode);
        //  上38，下40，左37，右39,空格32
        switch(keyCode){
            case 38:
                frame.heroPlane.up = true;
                break;
            case 40:
                frame.heroPlane.down = true;
                break;
            case 37:
                frame.heroPlane.left = true;
                break;
            case 39:
                frame.heroPlane.right = true;
                break;
            case 32:
                addBullet();
                break;
        }
    }

    //  键盘松开
    @Override
    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        switch(keyCode){
            case 38:
                frame.heroPlane.up = false;
                break;
            case 40:
                frame.heroPlane.down = false;
                break;
            case 37:
                frame.heroPlane.left = false;
                break;
            case 39:
                frame.heroPlane.right = false;
                break;
        }
    }

    /*
    * 装弹
    * */
    public void addBullet(){
        frame.bullets.add(new Bullet(frame.heroPlane.x,frame.heroPlane.y-20));
    }
}
